# -*- coding: iso-8859-15 -*-
import moteur
import SETTINGS
from Tkinter import *


class Vue(object):
    def __init__(self, ctrl):
        self.ctrl = ctrl
        self.root = Tk()
        self.frameSettings()
        self.canvas = Canvas(self.root, width=SETTINGS.LARGEUR, height=SETTINGS.HAUTEUR, bd=2, relief=GROOVE)
        self.canvas.pack()
        self.camera = moteur.Camera(SETTINGS.XCAM,SETTINGS.YCAM,SETTINGS.ZCAM)
        self.camera.centrerOeil(SETTINGS.HAUTEUR, SETTINGS.LARGEUR)
        self.camera.milieu.x = SETTINGS.DISPERSION/2
        self.camera.milieu.y = SETTINGS.DISPERSION/2
        self.camera.milieu.z = SETTINGS.DISPERSION/2
        self.root.bind("<Key-+>", self.zoomIn)
        self.root.bind("<Key-->", self.zoomOut)
        self.canvas.bind("<B3-Motion>", self.rotate)
        self.canvas.bind("<B1-Motion>", self.bouger)
        self.mouseX = 0
        self.mouseY = 0
        
        
        
        
    def frameSettings(self):
        settings = Frame(self.root)
        lperception = Label(settings, text="Distance de perception:").grid(row=0, column=0)
        self.tperception = Entry(settings)
        self.tperception.grid(row=0, column=1)
        ldistanceConfort = Label(settings, text="Distance de confort:").grid(row=1, column=0)
        self.tdistanceConfort = Entry(settings)
        self.tdistanceConfort.grid(row=1, column=1)
        lmilieu = Label(settings, text="Ajustement vers milieu:").grid(row=2, column=0)
        self.tmilieu = Entry(settings)
        self.tmilieu.grid(row=2, column=1)
        lajustementDirection = Label(settings, text="Ajustement de direction:").grid(row=3, column=0)
        self.tajustementDirection = Entry(settings)
        self.tajustementDirection.grid(row=3, column=1)
        lajustementConfort = Label(settings, text="Ajustement de confort:").grid(row=4, column=0)
        self.tajustementConfort = Entry(settings)
        self.tajustementConfort.grid(row=4, column=1)
        lvitesse = Label(settings, text="Vitesse:").grid(row=5, column=0)
        self.tvitesse = Entry(settings)
        self.tvitesse.grid(row=5, column=1)
        
        self.tperception.bind("<Return>", self.perception)
        self.tperception.insert(0, str(SETTINGS.PERCEPTION_INDIVIDUS))
        self.tdistanceConfort.bind("<Return>",self.distanceConfort)
        self.tdistanceConfort.insert(0, str(SETTINGS.DISTANCE_CONFORT))
        self.tmilieu.bind("<Return>",self.milieu)
        self.tmilieu.insert(0, str(SETTINGS.AJUSTEMENT_MILIEU))
        self.tajustementDirection.bind("<Return>",self.ajustementDirection)
        self.tajustementDirection.insert(0, str(SETTINGS.AJUSTEMENT_DIRECTION))
        self.tajustementConfort.bind("<Return>",self.ajustementConfort)
        self.tajustementConfort.insert(0, str(SETTINGS.AJUSTEMENT_CONFORT))
        self.tvitesse.bind("<Return>",self.vitesse)
        self.tvitesse.insert(0, str(SETTINGS.VITESSE_INDIVIDUS))
        
        settings.pack()
        
    def perception(self, event):
        SETTINGS.PERCEPTION_INDIVIDUS = int(self.tperception.get())
        self.root.focus()
    def distanceConfort(self, event):
        SETTINGS.DISTANCE_CONFORT = int(self.tdistanceConfort.get())
        self.root.focus()
    def milieu(self, event):
        SETTINGS.AJUSTEMENT_MILIEU = float(self.tmilieu.get())
        self.root.focus()
    def ajustementDirection(self, event):
        SETTINGS.AJUSTEMENT_DIRECTION = float(self.tajustementDirection.get())
        self.root.focus()
    def ajustementConfort(self, event):
        SETTINGS.AJUSTEMENT_CONFORT = float(self.tajustementConfort.get())
        self.root.focus()
    def vitesse(self, event):
        SETTINGS.VITESSE_INDIVIDUS = int(self.tvitesse.get()) 
        self.root.focus()

        
    def zoomOut(self, event):
        self.camera.bougerZ(-100)
        
    def zoomIn(self, event):
        self.camera.bougerZ(100)
        
    def rotate(self, event):
        if event.x > self.mouseX+2:
            self.camera.yawer(-0.02)
        elif event.x < self.mouseX-2:
            self.camera.yawer(0.02)
           
        if event.y > self.mouseY+2:
            self.camera.pitcher(-0.02)
        elif event.y < self.mouseY-2:
            self.camera.pitcher(0.02)
        self.mouseX = event.x
        self.mouseY = event.y
        
    def bouger(self, event):
        if event.x > self.mouseX+2:
            self.camera.bougerX(-20)
        elif event.x < self.mouseX-2:
            self.camera.bougerX(20)
           
        if event.y > self.mouseY+2:
            self.camera.bougerY(20)
        elif event.y < self.mouseY-2:
            self.camera.bougerY(-20)
        self.mouseX = event.x
        self.mouseY = event.y

        
    def afficher(self):
        self.canvas.delete(ALL)
        flock = self.ctrl.getFlock()
        liste = moteur.toEcran(self.camera, flock)
        for individu in liste:
            distance = self.camera.z - individu.z
            if distance <> 0:
                self.canvas.create_rectangle(individu.x, individu.y, individu.x+(1000/-distance)+2, individu.y+(1000/-distance)+2, fill = SETTINGS.COULEUR)
        liste = self.ctrl.getPredateurs()
        liste = moteur.toEcran(self.camera, liste)
        #for pred in liste:
        #    self.canvas.create_oval(pred.x, pred.y, pred.x+pred.z/50+5, pred.y+pred.z/50+5)